<?php
class Blog_AlbumController extends Zendvn_Controller_Action{
	
	public function init()
	{
		parent::init();
		
		$this->createTemplate(APPLICATION_PATH . '/templates/ajax','template.ini','template');
	}
	
	public function indexAction(){
		
		

		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		if($this->getRequest()->isPost()){
			
			
			$name = @$_POST["name"];
			
			if($name != ""){
				
				$blog = new Blog_Model_Index();
				
				$arr = array(
					"userid"=>$data->id,
					"name"=>$name,
					"createdate"=>date('Y-m-d h:i:s',time()),										
				);
				
				$blog->insertAlbum($arr);
				
			}
			
		}
		
	}
	
	public function uploadAction(){
		
		if($this->getRequest()->isPost()){
			
			$storage = new Zend_Auth_Storage_Session();
			$data = $storage->read();
		
			$albumid = @$_POST["albumid"];
				
			$upload = new Zend_File_Transfer_Adapter_Http();
			
			$path = realpath(APPLICATION_PATH."/../album/");
			
			
			$files = $upload->getFileInfo();
			
			$upload->setDestination($path);
			
			$i=1;
			
			foreach($files as $file => $info) {
				
				  $file_name = $upload->getFileName($info['name']);
				  
				  preg_match("/\.([^\.]+)$/", $file_name, $matches); 
		          $file_ext = $matches[1];
		          $file_name = $i.time() . '.' . $file_ext;
		          $upload->addFilter('Rename', $path .'/' . $file_name);
				
				if (!$upload->isValid()) 
				{
				    throw new Exception('Bad image data: '.implode(',', $upload->getMessages()));
				}
				
				try {
					
					
			        $upload->receive($file);
			        
			        $arr = array(
					"userid"=>$data->id,
			        "albumid"=>(int)$albumid,
					"img"=>$file_name,
					"description"=>"",	
			        "createdate"=>date('Y-m-d h:i:s',time()),	        										
					);
					
					$albumuser = new Blog_Model_AlbumUser();
					
					$albumuser->insertAlbumUser($arr);
					
					$i++;
					    
				} 
				catch (Zend_File_Transfer_Exception $e) 
				{
				        throw new Exception('Bad image data: '.$e->getMessage());
				}
				
				
		
			}
			
			
			
		
		}
		
	}
	
	
	
}